from common.base_api import BaseApi
from common.utils import Utils
from common.wrapper import api_call


class ProductApi(BaseApi):

    # ---------------------全链峰首页-------------------------
    @api_call
    def list_api(self):
        """
        商品分类
        :return:
        code:number
        message: string
        data:  object
        """
        api = self.conf_data['api']['product']['list']
        data = {
            'url': self.host + api,
            'method': 'get',
            'headers': self.headers,
            'json': {}
        }
        response = self.send_http(data)
        return response

    @api_call
    def hot_product_api(self):
        """
        首页配置数据热门作品
        :return:
        code:number
        message: string
        data:  object
        """
        api = self.conf_data['api']['product']['hot_product']

        data = {
            'url': self.host + api,
            'method': 'get',
            'headers': self.headers,
            'json': {}
        }
        response = self.send_http(data)
        return response

    @api_call
    def home_page_api(self):
        """
                首页配置数据banner
                :return:
                code:number
                message: string
                data:  object
                """
        api = self.conf_data['api']['product']['home_page']

        data = {
            'url': self.host + api,
            'method': 'get',
            'headers': self.headers,
            'json': {}
        }
        response = self.send_http(data)
        return response

    @api_call
    def creator_api(self):
        """
        首页配置推荐作者
        :return:
        """
        api = self.conf_data['api']['product']['creator']

        data = {
            'url': self.host + api,
            'method': 'get',
            'headers': self.headers,
            'json': {}
        }
        response = self.send_http(data)
        return response

    @api_call
    def hot_products_api(self):
        """
        首页热门作品集
        :return:
        """
        api = self.conf_data['api']['product']['hot_products']
        data = {
            'url': self.host + api,
            'method': 'get',
            'headers': self.headers,
            'json': {}
        }
        response = self.send_http(data)
        return response

    @api_call
    def guide_api(self):
        """
        首页向导
        :return:
        """
        api = self.conf_data['api']['product']['guide']
        data = {
            'url': self.host + api,
            'method': 'get',
            'headers': self.headers,
            'json': {}
        }
        response = self.send_http(data)
        return response

    @api_call
    def recommend_product_api(self):
        """
        首页推荐作品
        :return:
        """
        api = self.conf_data['api']['product']['recommend_product']
        data = {
            'url': self.host + api,
            'method': 'get',
            'headers': self.headers,
            'json': {}
        }
        response = self.send_http(data)
        return response

    # ---------------------全链峰市场页-------------------------

    @api_call
    def search_api(self, pageSize, pageNumber, **kwargs):
        """
        NFT搜索
        :param categoryId:分类ID 非必须
        :param pageSize: 条数 必须
        :param pageNumber:页码 必须
        :param minPrice: 最低价格 非必须
        :param maxPrice: 最高价格 非必须
        :param productSeriesId: 系列ID 非必须
        :param createUserId: 创建人ID 非必须
        :param ownerUserId:持有者ID 非必须
        :param sort:排序字段 非必须
        :param sortType: 排序方式： 升序 asc  降序 desc
        :param nftName: NFT名称
        :return:
        1.code：非必须
        2.message：非必须
        3.data：非必须
        """
        api = self.conf_data['api']['product']['search']
        data = {
            'url': self.host + api,
            'method': 'get',
            'headers': self.headers,
            'params': {
                "pageSiz": pageSize,
                "pageNumber": pageNumber
            }
        }
        data["data"].update(kwargs)
        response = self.send_http(data)
        return response

    @api_call
    def details_api(self, productId):
        """
        商品详情#基础数据
        :param productId:  商品id 必须
        :return:
        """
        api = self.conf_data['api']['product']['details']
        data = {
            'url': self.host + api,
            'method': 'get',
            'headers': self.headers,
            'params': {
                "productId": productId
            }
        }
        response = self.send_http(data)
        return response

    @api_call
    def tradeTrace_api(self, productId):
        """
        商品详情#交易溯源
        :param productId: 商品id 必须
        :return:
        """
        api = self.conf_data['api']['product']['tradeTrace']
        data = {
            'url': self.host + api,
            'method': 'get',
            'headers': self.headers,
            'params': {
                "productId": productId
            }
        }
        response = self.send_http(data)
        return response

    @api_call
    def priceTrend_api(self, productId):
        """
        商品详情#价格走势
        :param productId: 商品id 必须
        :return:
        """
        api = self.conf_data['api']['product']['priceTrend']
        data = {
            'url': self.host + api,
            'method': 'get',
            'headers': self.headers,
            'params': {
                "productId": productId
            }
        }
        response = self.send_http(data)
        return response

    @api_call
    def saleuserreward_api(self, productId):
        """
        作品的打赏用户列表
        :param productId:
        :return: 商品id 必须
        """
        api = self.conf_data['api']['product']['saleuserreward']
        data = {
            'url': self.host + api,
            'method': 'get',
            'headers': self.headers,
            'params': {
                "productId": productId
            }
        }
        response = self.send_http(data)
        return response

    @api_call
    def rewardPollen_api(self, productId, productSaleId, pollenNum, payPassword, token):
        """
        商品详情#NFT打赏花粉
        :param productId: 	number，商品ID， 必须
        :param productSaleId: number 售卖单ID 必须
        :param pollenNum:  number 打赏花粉数量 必须
        :param payPassword: string 支付密码 必须
        :return:
        """
        api = self.conf_data['api']['product']['rewardPollen']
        headers = self.headers
        headers["Authorization"] = token
        data = {
            'url': self.host + api,
            'method': 'post',
            'headers': headers,
            'json': {
                "productId": productId,
                "productSaleId": productSaleId,
                "pollenNum": pollenNum,
                "payPassword": payPassword
            }
        }
        response = self.send_http(data)
        return response

    @api_call
    def imageUpload_api(self, headers: dict, file_name, type):
        """
        图片二进制，请求upload接口，获取返回字段data
        :param headers: 请求的 headers
        :param file_name: 需要上传的文件名称
        :param type:  上传的文件的类型
        ex: file_name : "1.jpg"
            type : "image/jpeg"
        :return:
        """
        api = self.conf_data['api']['product']['imageUpload']
        response = Utils.get_upload_binary(url=self.host + api, headers=headers, file_name=file_name, type=type)
        return response

    @api_call
    def productSeries_list_api(self, token):
        """
        个人/他人空间#创建的系列列表
        :param token:
        :return:
        """
        api = self.conf_data['api']['product']['productSeries_list']
        headers = self.headers
        headers["Authorization"] = token
        data = {
            'url': self.host + api,
            'method': 'get',
            'headers': headers,
            'json': {
            }
        }
        response = self.send_http(data)
        return response

    @api_call
    def create_api(self, createType: str, product_type: str, product_originUrl: str,
                   product_name: str,
                   product_number: int, product_categoryId: int, product_coverImageUrl: str,
                   product_price: int, product_copyrightRatio: int, product_originFileSize: str, payPassword: int,
                   payNectar: int, token: str,
                   product_blockchainChecked=True, product_trustedTimestampChecked=False, **kwargs):
        """
       :param product_number: 1       铸造数量 (单份铸造传1，多份铸造就是具体的份数) 必须
       :param createType: SINGLE      铸造类型SINGLE:单个；MORE：多份 必须
       :param productSeries_id:       系列ID 必须 （不需要创建系列的时候传，要调用case_productSeries取到当前用户创建的系列的系列ID）
       :param productSeries_name        系列名称  （新创建系列的时候传） 必须
       :param productSeries_description  系列描述 （新创建系列的时候传）必须
       :param productSeries_coverUrl   系列封面 （新创建系列的时候传）必须
       :param product_type: AUDIO        NFT类型 PIC:图片，AUDIO:音频，VIDEO:视频，TEXT:文本,TICKET:票卷
       :param product_coverImageUrl: 作品封面 必须
       :param product_originUrl:  作品原件 必须
       :param product_name:  作品名称   必须
       :param product_signature:  作品描述  非必须
       :param product_price:   作品价格   必须
       :param product_copyrightRatio:  版税   必须
       :param product_categoryId:  作品分类    必须
       :param product_originFileSize:  原件大小
       :param payPassword:  支付密码
       :param payNectar:  0.00  消耗的花蜜
       :param product_trustedTimestampChecked:  时间戳   默认false
       :param product_blockchainChecked  区块链存储V2.0固定传值：默认true
       :param product_tags  作品标签  多个用空格隔开 非必须
       :param product_withNft  false 是否捆绑实物的NFT； 非必须 true代表是
       :param product_forbidSecondSale  禁止转售;  非必须，true代表是
       :param product_showUsedState  显示原件使用状态  非必须  true代表是
       :param product_nftContent  需加密内容 (铸造类型为文本和票券的时候需要传)
       :return:
       """

        api = self.conf_data['api']['product']['create']
        headers = {'Authorization': token}
        data = {
            'url': self.host + api,
            'method': 'post',
            'headers': headers,
            'data': {
                "product.number": product_number,
                "createType": createType,
                "product.type": product_type,
                "product.coverImageUrl": product_coverImageUrl,
                "product.originUrl": product_originUrl,
                "product.name": product_name,
                "product.price": product_price,
                "product.copyrightRatio": product_copyrightRatio,
                "product.categoryId": product_categoryId,
                "product.originFileSize": product_originFileSize,
                "payPassword": payPassword,
                "payNectar": payNectar,
                "product.trustedTimestampChecked": product_trustedTimestampChecked,
                "product.blockchainChecked": product_blockchainChecked

            }
        }

        data["data"].update(kwargs)
        response = self.send_http(data)
        return response

    @api_call
    def productSale_recommend_api(self, productId, limit=3):
        """
        详情页相关推荐
        :param productId:  当前商品ID 必须
        :param limit: 推荐数量，默认为3 必须
        :return:
        """
        api = self.conf_data['api']['product']['productSale_recommend']
        data = {
            'url': self.host + api,
            'method': 'get',
            'headers': self.headers,
            'params': {
                "productId": productId,
                "limit": limit
            }
        }
        response = self.send_http(data)
        return response

    # ---------------------全链峰创作者页-------------------------
    @api_call
    def creator_list_api(self, userType):
        """
        创作者页面获取创作者列表接口
        :param userType:  ARTIST创作者 ORGANIZATION创作机构
        :param pageNumber: 要查询的页数
        :param pageSize: 要展示的个数
        :return:
        """
        api = self.conf_data['api']['product']['creator_list']
        data = {
            'url': self.host + api,
            'method': 'get',
            'headers': self.headers,
            'params': {
                "userType": userType
            }
        }
        response = self.send_http(data)
        return response

    # ---------------------全链峰个人空间页-------------------------
    @api_call
    def info_api(self, userId):
        """
        查看艺术家/机构
        :param userId: 用户的ID 必须
        :return:
        """
        api = self.conf_data['api']['product']['info']
        data = {
            'url': self.host + api,
            'method': 'get',
            'headers': self.headers,
            'params': {
                "userId": userId
            }
        }
        response = self.send_http(data)
        return response

    @api_call
    def ownerList_api(self, status, token,**kwargs):
        """
        个人/他人空间#持有的作品
        :param staus: 挂售状态UP:上架;DOWN:下架;IN_AUCTION:拍卖中  必须
        :param userId:  用户ID  非必须
        :return:
        """
        api = self.conf_data['api']['product']['ownerList']
        headers = self.headers
        headers["Authorization"] = token
        data = {
            'url': self.host + api,
            'method': 'get',
            'headers': headers,
            'params': {
                "status": status

            }
        }
        data['params'].update(kwargs)
        response = self.send_http(data)
        return response

    @api_call
    def productSeries_createList_api(self, token, userId):
        """
        个人/他人空间#创建的系列列表
        :param token: 登录全链峰的token
        :param userId: 要查询的用户的ID
        :return:
        """
        api = self.conf_data['api']['product']['productSeries_createlist']
        headers = self.headers
        headers["Authorization"] = token
        data = {
            'url': self.host + api,
            'method': 'get',
            'headers': headers,
            'params': {
                "userId": userId
            }
        }
        response = self.send_http(data)
        return response

    @api_call
    def product_list_api(self, token, userId):
        """
        个人/他人空间#创建的作品
        :param token: 登录全链峰的token
        :param userId: 要查询的用户的ID
        :return:
        """
        api = self.conf_data['api']['product']['product_list']
        headers = self.headers
        headers["Authorization"] = token
        data = {
            'url': self.host + api,
            'method': 'get',
            'headers': headers,
            'params': {
                "userId": userId
            }
        }
        response = self.send_http(data)
        return response

    @api_call
    def attentionList_api(self, token, userId):
        """
        个人/他人空间#关注的作品
        :param token: 登录全链峰的token
        :param userId: 要查询的用户的ID
        :return:
        """
        api = self.conf_data['api']['product']['attentionList']
        headers = self.headers
        headers["Authorization"] = token
        data = {
            'url': self.host + api,
            'method': 'get',
            'headers': headers,
            'params': {
                "userId": userId
            }
        }
        response = self.send_http(data)
        return response

    # ---------------------全链峰个人中心页-------------------------

    @api_call
    def createList_api(self, token):
        """
        NFT铸造#列表
        :param token: 登录全链峰的token
        :return:
        """
        api = self.conf_data['api']['product']['createList']
        headers = self.headers
        headers["Authorization"] = token
        data = {
            'url': self.host + api,
            'method': 'get',
            'headers': headers,
            'params': {

            }
        }
        response = self.send_http(data)
        return response

    @api_call
    def productSale_list_api(self, token, status):
        """
        个人空间-我的挂售单
        :param token: 登录全链峰的token
        :param status: 状态，UP:挂售中,1:DOWN,2:DONE,(空字符串):全部
        :return:
        """
        api = self.conf_data['api']['product']['productSale_list']
        headers = self.headers
        headers["Authorization"] = token
        data = {
            'url': self.host + api,
            'method': 'get',
            'headers': headers,
            'params': {
                "status": status
            }
        }
        response = self.send_http(data)
        return response

    @api_call
    def order_api(self, token, orderType, orderState):
        """
        个人空间-我的购买订单
        :param token: 登录全链峰的token
        :param orderType: 订单类型[NFT:nft铸造;DEAL:交易订单]
        :param orderState: 订单状态[WAIT_PAY:待支付;
                          PAY_FAILURE:已过期;COMPLETE:完成;CANCEL:已取消,空：全部]
        :return:
        """
        api = self.conf_data['api']['product']['order']
        headers = self.headers
        headers["Authorization"] = token
        data = {
            'url': self.host + api,
            'method': 'get',
            'headers': headers,
            'params': {
                "orderType": orderType,
                "orderState": orderState
            }
        }
        response = self.send_http(data)
        return response

    @api_call
    def settlement_list_api(self, token):
        """
        结算数据列表
        :param token: 登录全链峰的token
        :return:
        """
        api = self.conf_data['api']['product']['settlement_list']
        headers = self.headers
        headers["Authorization"] = token
        data = {
            'url': self.host + api,
            'method': 'get',
            'headers': headers,
            'params': {

            }
        }
        response = self.send_http(data)
        return response

    @api_call
    def rewardPollenList_api(self, token):
        """
        用户花粉打赏列表
        :param token: 登录全链峰的token
        :return:
        """
        api = self.conf_data['api']['product']['rewardPollenList']
        headers = self.headers
        headers["Authorization"] = token
        data = {
            'url': self.host + api,
            'method': 'get',
            'headers': headers,
            'params': {

            }
        }
        response = self.send_http(data)
        return response

    @api_call
    def canSaleList_api(self, token):
        """
        NFT挂售#选择可列挂售商品列表
        :param token:
        :return:
        """
        api = self.conf_data['api']['product']['canSaleList']
        headers = self.headers
        headers["Authorization"] = token
        data = {
            'url': self.host + api,
            'method': 'get',
            'headers': headers,
            'params': {

            }
        }
        response = self.send_http(data)
        return response

    @api_call
    def price_calculate_api(self, productId):
        """
        NFT挂售#价格变动计算
        :param productId:商品id number 必须
        :param price: 非必须  number 价格
        :param rewardPoolRatio: 非必须  number 打赏池比例
        :return:
        """
        api = self.conf_data['api']['product']['price_calculate']

        data = {
            'url': self.host + api,
            'method': 'post',
            'headers': self.headers,
            'json': {"productId": productId

                     }
        }
        response = self.send_http(data)
        return response

    @api_call
    def up_api(self, token, productId, price, payPassword):
        """
        NFT挂售#上架
        :param token:
        :param productId:  product.id  number 必须
        :param price: 挂售价格  number 必须
        :param payPassword:  支付密码   number 必须
        :return:
        """
        api = self.conf_data['api']['product']['up']
        headers = self.headers
        headers["Authorization"] = token
        data = {
            'url': self.host + api,
            'method': 'post',
            'headers': headers,
            'json': {"productId": productId,
                     "price": price,
                     "payPassword": payPassword

                     }
        }
        response = self.send_http(data)
        return response

    @api_call
    def down_api(self, token, productSaleId,  payPassword):
        """
        NFT挂售#下架
        :param token:
        :param productId:  product.id  number 必须
        :param price: 挂售价格  number 必须
        :param payPassword:  支付密码   number 必须
        :return:
        """
        api = self.conf_data['api']['product']['down']
        headers = self.headers
        headers["Authorization"] = token
        data = {
            'url': self.host + api,
            'method': 'post',
            'headers': headers,
            'json': {"productSaleId": productSaleId,
                     "payPassword": payPassword

                     }
        }
        response = self.send_http(data)
        return response